package com.hontech.kaltaapp.device.codec

import com.hontech.kaltaapp.utils.toU8



class Uint8(var value: Int = 0) : CodecType {

    override fun size(): Int {
        return 1
    }

    override fun encode(buf: ByteArray, index: Int) {
        buf[index] = this.value.toByte()
    }

    override fun decode(buf: ByteArray, index: Int) {
        this.value = buf[index].toU8()
    }
}

class Uint16(var value: Int = 0) : CodecType {

    override fun size(): Int {
        return 2
    }

    override fun encode(buf: ByteArray, index: Int) {
        buf[index] = (this.value shr 8).toByte()
        buf[index + 1] = this.value.toByte()
    }

    override fun decode(buf: ByteArray, index: Int) {
        this.value = (buf[index].toU8() shl 8) + buf[index + 1].toU8()
    }
}

class Uint32(var value: Int = 0) : CodecType {

    override fun size(): Int {
        return 4
    }

    override fun encode(buf: ByteArray, index: Int) {
        buf[index] = (this.value shr 24).toByte()
        buf[index + 1] = (this.value shr 16).toByte()
        buf[index + 2] = (this.value shr 8).toByte()
        buf[index + 3] = this.value.toByte()
    }

    override fun decode(buf: ByteArray, index: Int) {
        this.value = (buf[index].toU8() shl 24) +
                (buf[index + 1].toU8() shl 16) +
                (buf[index + 2].toU8() shl 8) +
                buf[index + 3].toU8()
    }
}

fun Int.u8() = Uint8(this)
fun Int.u16() = Uint16(this)
fun Int.u32() = Uint32(this)



